#!/usr/bin/env bash
set -e

function error {
  echo Cheerio "prepublish" script: $1 >&2
  false
}

if [ "$VERSION" == "" ] ; then
  error 'VERSION environment variable unset.'
fi

if [ "$PREVIOUS_VERSION" == "" ] ; then
  error 'PREVIOUS_VERSION environment variable unset.'
fi

if ! grep -E "^$VERSION / [0-9]{4}-[0-9]{2}-[0-9]{2}" History.md > /dev/null; then
  error "No entry for version $VERSION found in History.md file. (This may be seeded with the generate_history.sh script.)"
fi

if ! git diff-index --quiet HEAD -- History.md; then
  error 'History.md file contains untracked changes. Commit them prior to publication.'
fi
